home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / States.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  5.6 KB  |  248 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  States and Capitals module
  3.  *
  4.  * This module is an example of an extremely simple interrogative
  5.  * scenario.  The original thirteen colonies of the United States must
  6.  * be matched with their capital cities.  There is a single button that
  7.  * says "Give Up?"  If the user clicks the button, the answer is shown,
  8.  * the button's title changes to "Resume", and the scenario is frozen
  9.  * until the button is clicked.  Then the answer is cleared, the capitals
  10.  * are shuffled, and the user may start over.
  11.  *
  12.  * If the correct answers are all gotten, the button again changes to
  13.  * "Resume" and the scenario is frozen until the button is clicked.
  14.  *
  15.  * The module is unexciting; it's meant as a basic illustration, rather
  16.  * than anything useful.  There are lots of bells and whistles that
  17.  * could be added.
  18.  *
  19.  * 26 July 1986        Paul DuBois
  20.  *
  21.  * 29 Dec 93
  22.  * - Convert blobs from picture blobs to procedure blobs.
  23.  * This gives better dimming on monitor that can do grayscale
  24.  * and turns out to be a lot faster.
  25. */
  26.  
  27. # include    "TransSkel.h"
  28.  
  29. # include    "BlobMgr.h"
  30. # include    "BlobDemo.h"
  31.  
  32.  
  33. # define    nStates        13
  34.  
  35. # define    hState1        220
  36. # define    hState2        110
  37. # define    vState        16
  38. # define    stateXOff    10
  39. # define    stateYOff    5
  40. # define    hCapital    85
  41. # define    vCapital    16
  42. # define    capXOff        225
  43. # define    capYOff        5
  44.  
  45.  
  46. struct info
  47. {
  48.     unsigned char    *stateName;
  49.     unsigned char    *capName;
  50. };
  51.  
  52.  
  53. static WindowPtr        wind;
  54. static FontInfo            fontInfo;
  55. static BlobSetHandle    donors;        /* donor blobs */
  56. static BlobSetHandle    receptors;    /* receptor blobs */
  57. static ControlHandle    giveUp;
  58. static Boolean            paused = false;
  59.  
  60. static struct info        scPair[nStates] =
  61. {
  62.     { "\pConnecticut",        "\pHartford" },
  63.     { "\pDelaware",            "\pDover" },
  64.     { "\pGeorgia",            "\pAtlanta" },
  65.     { "\pMaryland",            "\pAnnapolis" },
  66.     { "\pMassachusetts",    "\pBoston" },
  67.     { "\pNew Hampshire",    "\pConcord" },
  68.     { "\pNew Jersey",        "\pTrenton" },
  69.     { "\pNorth Carolina",    "\pRaleigh" },
  70.     { "\pNew York",            "\pAlbany" },
  71.     { "\pPennsylvania",        "\pHarrisburg" },
  72.     { "\pRhode Island",        "\pProvidence" },
  73.     { "\pSouth Carolina",    "\pColumbia" },
  74.     { "\pVirginia",            "\pRichmond" }
  75. };
  76.  
  77.  
  78. static void
  79. DrawBlobString (Rect *r, StringPtr str)
  80. {
  81. short    h, v;
  82.  
  83.     h = (r->left + r->right - StringWidth (str)) / 2;
  84.     v = r->bottom - fontInfo.descent;
  85.     MoveTo (h, v);
  86.     DrawString (str);
  87. }
  88.  
  89. /*
  90.  * Donor blobs have empty static regions, so this is only called to draw
  91.  * the drag region (the capital name).
  92.  */
  93.  
  94. static pascal void
  95. DrawDonor (BlobHandle bDst, BlobHandle bSrc, short partCode)
  96. {
  97. Rect    r;
  98.  
  99.     r = BDragBox (bDst);
  100.     EraseRect (&r);
  101.     DrawBlobString (&r, scPair[GetBRefCon (bSrc)].capName);
  102. }
  103.  
  104.  
  105. static pascal void
  106. DrawReceptor (BlobHandle bDst, BlobHandle bSrc, short partCode)
  107. {
  108. Rect    r;
  109.  
  110.     if (partCode == inDragBlob)
  111.     {
  112.         r = BDragBox (bDst);
  113.         EraseRect (&r);
  114.         FrameRect (&r);
  115.     }
  116.     else
  117.     {
  118.         r = BStatBox (bDst);
  119.         EraseRect (&r);
  120.         DrawBlobString (&r, scPair[GetBRefCon (bSrc)].stateName);
  121.     }
  122. }
  123.  
  124.  
  125. static void
  126. MakeBlobs (void)
  127. {
  128. short        i;
  129. Rect        r, r2;
  130. BlobHandle    b1, b2;
  131.  
  132.     donors = NewBlobSet ();
  133.     receptors = NewBlobSet ();
  134.     for (i = 0; i < nStates; ++i)
  135.     {
  136.         b1 = NewBlob (donors, true, 1, false, (long) i);
  137.         b2 = NewBlob (receptors, true, 0, true, (long) i);
  138.         NewBlobMatch (b1, b2);        /* attach the answer */
  139.  
  140.         SetRect (&r, 0, 0, hCapital, vCapital);
  141.         SetProcRectBlob (b1, DrawDonor, &r, &r);
  142.         MoveBlob (b1, inFullBlob, capXOff, capYOff + (vCapital + 2) * i);
  143.  
  144.         SetRect (&r, 0, 0, hState2, vState);
  145.         SetRect (&r2, 0, 0, hCapital, vCapital);
  146.         OffsetRect (&r2, hState2 + 5, 0);
  147.         SetProcRectBlob (b2, DrawReceptor, &r2, &r);
  148.         MoveBlob (b2, inFullBlob, stateXOff, stateYOff + (vState + 2) * i);
  149.     }
  150. }
  151.  
  152.  
  153. static pascal void
  154. Mouse (Point pt, long t, short mods)
  155. {
  156. BlobHandle    b, d;
  157. ControlHandle    ctl;
  158.  
  159.     if (FindControl    (pt, wind, &ctl))
  160.     {
  161.         if (TrackControl (ctl, pt, nil))
  162.         {
  163.             if (paused)        /* either all answers are correct, or we're */
  164.             {                /* showing the answer.  Either way, restore */
  165.                 ThawBlobSet (receptors);    /* to start state */
  166.                 ThawBlobSet (donors);
  167.                 ZUnglueGlobSet (receptors);
  168.                 ShuffleBlobSet (receptors);
  169.                 ShuffleBlobSet (donors);
  170.                 SetCTitle (giveUp, "\pGive Up?");
  171.                 ValidRect (&wind->portRect);
  172.                 paused = false;
  173.             }
  174.             else        /* user gives up - show answer */
  175.             {
  176.                 for (b = FirstBlob (receptors); b != nil; b = NextBlob (b))
  177.                     ZGlueGlob (FirstBMatch (b), b);
  178.                 FreezeBlobSet (receptors);
  179.                 FreezeBlobSet (donors);
  180.                 SetCTitle (giveUp, "\pResume");
  181.                 paused = true;
  182.             }
  183.         }
  184.     }
  185.     else
  186.     {
  187.         BlobClick (pt, t, donors, receptors);
  188.         if (!paused && BlobSetQuiet (receptors))    /* answers correct? */
  189.         {
  190.             FreezeBlobSet (receptors);
  191.             FreezeBlobSet (donors);
  192.             SetCTitle (giveUp, "\pResume");
  193.             paused = true;
  194.         }
  195.     }
  196. }
  197.  
  198.  
  199. static pascal void
  200. Update (Boolean resized)
  201. {
  202.     DrawControls (wind);
  203.     DrawBlobSet (receptors);
  204.     DrawBlobSet (donors);
  205. }
  206.  
  207.  
  208. static pascal void
  209. Activate (Boolean active)
  210. {
  211.     if (active)
  212.     {
  213.         SetDragRects (wind);
  214.         SetBCPermissions (true, true, false, true, true);
  215.     }
  216. }
  217.  
  218.  
  219. void
  220. StatesInit (void)
  221. {
  222. Rect    r;
  223.  
  224.     SkelWindow (wind = GetDemoWind (statesWindRes),
  225.                 Mouse,            /* mouse clicks */
  226.                 nil,            /* key clicks */
  227.                 Update,            /* updates */
  228.                 Activate,        /* activate/deactivate events */
  229.                 nil,            /* close window */
  230.                 DoWClobber,        /* dispose of window */
  231.                 nil,            /* idle proc */
  232.                 false);            /* irrelevant, since no idle proc */
  233.  
  234.     GetFontInfo (&fontInfo);
  235.  
  236.     MakeBlobs ();
  237.  
  238.     r = wind->portRect;
  239.     SetRect (&r, r.left + 5, r.bottom - 25, r.left + 75, r.bottom - 5);
  240.     giveUp = NewControl (wind, &r, "\pGive Up?", true, 0, 0, 0,
  241.                             pushButProc, 0L);
  242.  
  243.     MakeFrontWind (wind);        /* show window */
  244.     SkelPause (30L);            /* wait a bit */
  245.     ShuffleBlobSet (donors);    /* scramble answers */
  246.     SkelDoUpdates ();            /* process update resulting from scramble */
  247. }
  248.